define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'project/index' + location.search,
                    add_url: 'project/add',
                    edit_url: 'project/edit',
                    del_url: 'project/del',
                    multi_url: 'project/multi',
                    table: 'project',
                },
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'pno', title: __('Pno')},
                        // {field: 'sort', title: __('Sort')},
                        // {field: 'street_id', title: __('Street_id')},
                        {field: 'street_name', title: __('Street_name')},
                        {field: 'name', title: __('Name')},
                        {field: 'investment_total', title: __('Investment_total'), operate:'BETWEEN'},
                        {field: 'progression', title: __('Progression')},
                        {field: 'problem', title: __('Problem')},
                        {field: 'next_measure', title: __('Next_measure')},
                        {field: 'access_time', title: __('Access_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'access_time_desc', title: __('Access_time_desc')},
                        {field: 'sign_time', title: __('Sign_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'sign_time_desc', title: __('Sign_time_desc')},
                        {field: 'framework_agreement_time', title: __('Framework_agreement_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'framework_agreement_time_desc', title: __('Framework_agreement_time_desc')},
                        {field: 'invesment_agreement_time', title: __('Invesment_agreement_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'invesment_agreement_time_desc', title: __('Invesment_agreement_time_desc')},
                        {field: 'start_construction_time', title: __('Start_construction_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'start_construction_time_desc', title: __('Start_construction_time_desc')},
                        {field: 'register_time', title: __('Register_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'register_time_desc', title: __('Register_time_desc')},
                        {field: 'investment_area', title: __('Investment_area')},
                        {field: 'investment_name', title: __('Investment_name')},
                        {field: 'investment_is_listed', title: __('Investment_is_listed')},
                        {field: 'investment_is_soe', title: __('Investment_is_soe')},
                        {field: 'investment_is_soke', title: __('Investment_is_soke')},
                        {field: 'investment_is_global500', title: __('Investment_is_global500')},
                        {field: 'investment_is_china500', title: __('Investment_is_china500')},
                        {field: 'asset_type', title: __('Asset_type')},
                        {field: 'accountability_unit', title: __('Accountability_unit')},
                        {field: 'planned_street', title: __('Planned_street')},
                        {field: 'project_source_id', title: __('Project_source_id')},
                        {field: 'project_source_name', title: __('Project_source_name')},
                        {field: 'project_contact_mobile', title: __('Project_contact_mobile')},
                        {field: 'project_contact_nickname', title: __('Project_contact_nickname')},
                        {field: 'city_district_contact_mobile', title: __('City_district_contact_mobile')},
                        {field: 'progress_status_name', title: __('Progress_status')},
                        {field: 'five_industry_id', title: __('Five_industry_id')},
                        {field: 'five_industry_name', title: __('Five_industry_name')},
                        {field: 'industry_category_ids', title: __('Industry_category_ids')},
                        {field: 'industry_category_name', title: __('Industry_category_name')},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        discuss: function () {
            // 初始化表格参数配置
            // Table.api.init({
            //     // extend: {
            //     //     index_url: 'project/discuss' + location.search,
            //     //     add_url: 'project/discuss_add',
            //     //     edit_url: 'project/edit',
            //     //     // del_url: 'project/del',
            //     //     // multi_url: 'project/multi',
            //     //     table: 'project',
            //     // }
            // });

            Table.api.init();
            var table1 = $("#table1");

            // 初始化表格
            table1.bootstrapTable({
                extend: {
                    add_url: 'project/discuss_add',
                    edit_url: 'project/edit',
                    dragsort_url: 'ajax/weigh',
                },
                url: 'project/discuss' + location.search,
                pk: 'id',
                sortName: 'id',
                undefinedText:'',
                toolbar: "#toolbar1",
                singleSelect:true,
                // search:false,
                showToggle:false,
                columns: Controller.api.columns(table1,true,true)
            });

            // 为表格绑定事件
            Table.api.bindevent(table1);
            Controller.api.bindeventTable(table1);

            var table2 = $("#table2");

            // 初始化表格
            table2.bootstrapTable({
                url: 'project/discuss' + location.search,
                pk: 'id',
                sortName: 'id',
                toolbar: "#toolbar2",
                queryParams:function(params){
                    // console.log(params.filter);
                    params.filter = JSON.parse(params.filter);
                    params.op = JSON.parse(params.op);
                    params.filter.status = 1;
                    params.op.status = '=';

                    params.filter = JSON.stringify(params.filter);
                    params.op = JSON.stringify(params.op);
                    return params;
                },
                columns: Controller.api.check_columns(table2)
            });

            // 为表格绑定事件
            Table.api.bindevent(table2);

            // $("#btn-import-custom").click(function () {
            //     var url = 'project/importdiscuss';
            //     var icon = 'fa fa-pencil';
            //     Backend.api.addtabs(url, '编辑', icon);
            // });
            $("#btn-import-custom").click(function () {
                if(Config.isbinddep || Config.isSuperAdmin)
                {
                    var url = 'project/importdiscuss';
                    Fast.api.open(url, __('Import'), {});
                } else {
                    Layer.alert(__('您还未绑定部门，请先绑定'));
                }
            });



        },
        signed: function() {
            // 初始化表格参数配置
            // Table.api.init({
            //     extend: {
            //         index_url: 'project/signed' + location.search,
            //         add_url: 'project/add',
            //         edit_url: 'project/binedit',
            //         // del_url: 'project/del',
            //         // multi_url: 'project/multi',
            //         table: 'project',
            //     }
            // });
            Table.api.init();
            var table1 = $("#table1");

            // 初始化表格
            table1.bootstrapTable({
                extend: {
                    add_url: 'project/signed_add',
                    edit_url: 'project/edit',
                    dragsort_url: 'ajax/weigh',
                },
                url: 'project/signed' + location.search,
                pk: 'id',
                sortName: 'status desc,id desc',
                undefinedText:'',
                toolbar: "#toolbar1",
                // search:false,
                showToggle:false,
                singleSelect:true,
                columns: Controller.api.columns(table1,true,true)
            });

            // 为表格绑定事件
            Table.api.bindevent(table1);
            Controller.api.bindeventTable(table1);
            var table2 = $("#table2");

            // 初始化表格
            table2.bootstrapTable({
                url: 'project/signed' + location.search,
                pk: 'id',
                sortName: 'id',
                toolbar: "#toolbar2",
                queryParams:function(params){
                    // console.log(params);
                    params.filter = JSON.parse(params.filter);
                    params.op = JSON.parse(params.op);
                    params.filter.status = 1;
                    params.op.status = '=';

                    params.filter = JSON.stringify(params.filter);
                    params.op = JSON.stringify(params.op);
                    return params;
                },
                columns: Controller.api.check_columns(table2)
            });

            // 为表格绑定事件
            Table.api.bindevent(table2);

            $("#btn-import-custom").click(function () {
                if(Config.isbinddep || Config.isSuperAdmin)
                {
                    var url = 'project/importsigned';
                    Fast.api.open(url, __('Import'), {});
                } else {
                     Layer.alert(__('您还未绑定部门，请先绑定'));
                }
            });
            Controller.api.events.operate;
        },
        bymonth: function() {
            Table.api.init({
                extend: {
                    index_url: 'project/bymonth' + location.search,
                    // add_url: 'project/add',
                    // edit_url: 'project/edit',
                    // del_url: 'project/del',
                    // multi_url: 'project/multi',
                    table: 'project',
                }
            });

            var table = $("#table");
            // console.log('project/leader' + location.search);
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: Controller.api.columns(table,3)
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        leader: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'project/leader' + location.search,
                    add_url: 'project/add',
                    edit_url: 'project/edit',
                    del_url: 'project/del',
                    // multi_url: 'project/multi',
                    table: 'project',
                }
            });

            var table = $("#table");
            // console.log('project/leader' + location.search);
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                queryParams:function(params){
                    $.extend(params, {
                        ids: Config.ids
                    });
                    return params;
                },
                columns: Controller.api.columns(table,false,false)
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        activity: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'project/activity' + location.search,
                    table: 'project',
                }
            });

            var table = $("#table");


            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                queryParams:function(params){
                    $.extend(params, {
                        ids: Config.ids
                    });
                    return params;
                },
                columns: Controller.api.columns(table,false,false)

            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        province: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'project/province' + location.search,
                    table: 'project',
                }
            });

            var table = $("#table");


            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: Controller.api.columns(table,false,false)

            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        city: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'project/city' + location.search,
                    table: 'project',
                }
            });

            var table = $("#table");


            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',

                columns: Controller.api.columns(table,false,false)

            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        access: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'project/access' + location.search,
                    table: 'project',
                }
            });

            var table = $("#table");


            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                undefinedText:'',
                showColumns: false,
                showToggle: false,
                showExport: false,
                showSearch: true,
                commonSearch: true,
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'operate', title: __('Operate'), align:'right',table: table, events: Table.api.events.operate,
                            buttons: [
                               {
                                    name: 'detail',
                                    icon: 'fa fa-info',
                                    title: __('详情'),
                                    text: '详情',
                                    url: 'project/detail',
                                    extend: 'data-toggle="tooltip"',
                                    classname: 'btn btn-xs btn-info btn-addtabs'
                                }
                            ],
                            formatter: Table.api.formatter.buttons

                        },
                        {field: 'name', title: __('Name'),operate:'like'},
                        {field: 'investment_name', title: __('investment_name'),operate:'like'},
                        {field: 'investment_total', title: __('Investment_total'), operate:'BETWEEN',
                            formatter:function(value,row,index) {
                                if(value == -100)
                                    return '待定';
                                else
                                    return value;
                            }
                        },
                        {field: 'carrier_area', title: __('Carrier_area'),operate:false},
                        {field: 'summary', title: __('Summary'), width: '260px',operate:false},
                        {field: 'admission_meeting_time_desc', title: __('参加准入例会时间'), operate:false},
                        {field: 'special_meeting_time_desc', title: __('参加区委专题会时间'), operate:false},
                        {field: 'progress_status_id', title: __('项目状态'),
                            visible:false,
                            searchList: $.getJSON("selectlist/progress")
                        },
                        {field: 'progress_status_name', title: __('progress_status_name'),
                            operate:false,
                        },
                        {field: 'problem', title: __('Problem'),operate:false},
                        {field: 'tax_desc', title: __('税收约定及纳税情况说明'), operate:false},

                        {field: 'chargedep_names', title: __('责任部门'),operate:false},
                        {field: 'chargedep_ids', title: __('责任部门'),visible:false,
                                    operate:'FIND_IN_SET',
                                    searchList: $.getJSON("selectlist/index/tablename/chargedep")
                                    // extend:"data-source='chargedep/index'"
                        }


                    ]
                ]

            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        importdiscuss:function() {
            Controller.api.bindevent();
        },
        importsigned:function() {
            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();

        },
        edit: function () {
            Controller.api.bindevent();
            // Controller.api.custom_bindvent($("form[role=plan-form]"));
            Controller.api.custom_bindvent($("form[role=capital-form]"));
            // Controller.api.custom_bindvent($("form[role=formalities-form]"));

            $(".single_submit").click(function(event) {
                Controller.api.bindSubmitSingle(this, 1);
            });

            $(".single_draft").click(function(event) {
               Controller.api.bindSubmitSingle(this, 0);
            });

            $(".all_submit").click(function(event) {
                Controller.api.bindSubmitAll(1);
            })

            $(".all_draft").click(function(event) {
                Controller.api.bindSubmitAll(0);
            })

            $(".pending").click(function(){
                var item_id =$(this).data('id');
                if(this.checked)
                {
                    $("#"+item_id).val("待定");
                    $("#"+item_id).attr('readonly','readonly');
                }
                else
                {
                    $("#"+item_id).val("");
                    $("#"+item_id).removeAttr('readonly');
                }
            });
            //  $(".pending").click(function(){
            //     var item_id =$(this).data('id');
            //     if(this.checked)
            //     {
            //         $("#"+item_id).val("待定");
            //         $("#"+item_id).attr('readonly','readonly');
            //     }
            //     else
            //     {
            //         $("#"+item_id).val("");
            //         $("#"+item_id).removeAttr('readonly');
            //     }
            // });

            // $("#chargedep_ids").val(Config.chargedep_id);

            $('#carrier_id').change(function(event) {
                var value = $(this).val();
                var id = $(this).attr('id');

                var unit = '';
                var readonly = false;
                var required = null;
                switch (value) {
                    case "1":
                        unit = "亩";
                        readonly = false;
                        required = "required";
                        break;
                    case "2":
                    case "3":
                    case "4":
                        unit = "平米";
                        readonly = false;
                        required = "required";
                        break;
                    case "5":
                        unit = "";
                        readonly = true;
                        required = null;
                        break;
                }
                $("#"+id+"_relation").attr('readonly', readonly);
                $("#"+id+"_addon").html(unit);


            });

            var form = $("form[role=edit-form]")


            Table.api.init();

            // var table1 = $("#table1");

            // // 初始化表格
            // table1.bootstrapTable({
            //     url: 'project_plan/index' + location.search,
            //     pk: 'id',
            //     sortName: 'id',
            //     toolbar: "#toolbar1",
            //     search: true,
            //     showExport: false,
            //     showToggle: false,
            //     showColumns: false,
            //     commonSearch: true,
            //     searchFormVisible: false,
            //     showSearch: false,
            //     queryParams: function(params) {
            //         params.filter = JSON.parse(params.filter);
            //         params.op = JSON.parse(params.op);
            //         params.filter.project_id = Config.ids;
            //         params.op.project_id= '=';
            //         params.filter = JSON.stringify(params.filter);
            //         params.op = JSON.stringify(params.op);
            //         return params;
            //     },
            //     extend: {
            //         del_url: 'project_plan/del',
            //     },
            //     columns: [
            //         [
            //             {checkbox: true},
            //             {field: 'id', title: __('Id')},
            //             {field: 'plan_time', title: __('日期'), operate:false, formatter: Table.api.formatter.datetime,datetimeFormat: 'YYYY-M-D'},
            //             {field: 'plan_content', title: __('内容')},
            //             {field: 'status', title: __('状态')},
            //             {field: 'operate', title: __('Operate'), table: table1, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
            //         ]
            //     ]
            // });

            // // 为表格绑定事件
            // Table.api.bindevent(table1);

            var table2 = $("#table2");

            // 初始化表格
            table2.bootstrapTable({
                url: 'project_capital/index' + location.search,
                pk: 'id',
                sortName: 'id',
                toolbar: "#toolbar2",
                search: true,
                showExport: false,
                showToggle: false,
                showColumns: false,
                commonSearch: true,
                searchFormVisible: false,
                showSearch: false,
                queryParams: function(params) {
                    params.filter = JSON.parse(params.filter);
                    params.op = JSON.parse(params.op);
                    params.filter.project_id = Config.ids;
                    params.op.project_id= '=';
                    params.filter = JSON.stringify(params.filter);
                    params.op = JSON.stringify(params.op);
                    return params;
                },
                extend: {
                    del_url: 'project_plan/del',
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'gained_time', title: __('日期'), operate:false, formatter: Table.api.formatter.datetime,datetimeFormat: 'YYYY-M-D'},
                        {field: 'domestic_capital', title: __('内资资金')},
                        {field: 'foreign_capital', title: __('外资资金')},
                        {field: 'status', title: __('状态')},
                        {field: 'operate', title: __('Operate'), table: table2, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // var table3 = $("#table3");

            // // 初始化表格
            // table3.bootstrapTable({
            //     url: 'project_formalities/index' + location.search,
            //     pk: 'id',
            //     sortName: 'id',
            //     toolbar: "#toolbar3",
            //     search: true,
            //     showExport: false,
            //     showToggle: false,
            //     showColumns: false,
            //     commonSearch: true,
            //     searchFormVisible: false,
            //     showSearch: false,
            //     queryParams: function(params) {
            //         params.filter = JSON.parse(params.filter);
            //         params.op = JSON.parse(params.op);
            //         params.filter.project_id = Config.ids;
            //         params.op.project_id= '=';
            //         params.filter = JSON.stringify(params.filter);
            //         params.op = JSON.stringify(params.op);
            //         return params;
            //     },
            //     extend: {
            //         del_url: 'project_formalities/del',
            //     },
            //     columns: [
            //         [
            //             {checkbox: true},
            //             {field: 'id', title: __('Id')},
            //             {field: 'formalities_name', title: __('手续名称')},
            //             {field: 'handel_time', title: __('办理日期'), operate:false, formatter: Table.api.formatter.datetime,datetimeFormat: 'YYYY-M-D'},
            //             {field: 'status', title: __('状态')},
            //             {field: 'operate', title: __('Operate'), table: table3, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
            //         ]
            //     ]
            // });
            // Table.api.bindevent(table2);
            Controller.api.viewchecklog();
            // Controller.api.custom_validator($("form[role=plan-form]"), table1);
            Controller.api.custom_validator($("form[role=capital-form]"), table2);
            // Controller.api.custom_validator($("form[role=formalities-form]"), table3);

            $("#qdzrnd").click(function(){
                var that = this;
                Layer.confirm(__('确定要转入该年度吗?'), {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    $.ajax({
                        //几个参数需要注意一下
                        type: "POST",//方法类型
                        dataType: "json",//预期服务器返回的数据类型
                        url: "project/switchyear",
                        data: {pno: $(that).data("pno"),toyear:$("#zrnd").val(),fromyear:$(that).data('fromyear')},
                        success: function (ret) {
                            Layer.alert(__('提交成功'), function(index){
                                location.reload();
                            });
                        },
                        error : function(error) {
                            console.log(error);
                        }
                    });
                    Layer.close(index);
                });
            });

        },
        checkedit: function () {
            Controller.api.bindevent();

            $(".single_submit").click(function(event) {
                // console.log($(this).data("item_id"));
                var that = this;
                Layer.confirm(__('确定要通过审核吗?'), {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    $.ajax({
                        //几个参数需要注意一下
                        type: "POST",//方法类型
                        dataType: "json",//预期服务器返回的数据类型
                        url: "project/checkpass/ids/"+ Config.ids,
                        data: {item_id: $(that).data("item_id")},
                        success: function (ret) {
                            Layer.alert(__('提交成功'), function(index){
                                location.reload();
                            });
                        },
                        error : function(error) {
                            console.log(error);
                        }
                    });
                    Layer.close(index);
                });

            });
            $(".single_reject").click(function(event) {
                var that = this;
                var item_id = $(that).data("item_id");
                var reject_value = $("#"+item_id+'_reject_desc').val();

                var data = {};
                data['reject_desc'] = reject_value;
                data['item_id'] = item_id;
                Layer.confirm(__('确定要驳回吗?'), {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    $.ajax({
                        //几个参数需要注意一下
                        type: "POST",//方法类型
                        dataType: "json",//预期服务器返回的数据类型
                        url: "project/checkreject/ids/"+ Config.ids,
                        data: data,
                        success: function (ret) {
                            Layer.alert(__('驳回成功'), function(index){
                                location.reload();
                            });
                        },
                        error : function(error) {
                            console.log(error);
                        }
                    });
                    Layer.close(index);
                });


            });

            $(".all_submit").click(function(event) {
                // console.log($(this).data("item_id"));
                var that = this;
                Layer.confirm(__('确定所有要通过审核吗?'), {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    $.ajax({
                        //几个参数需要注意一下
                        type: "POST",//方法类型
                        dataType: "json",//预期服务器返回的数据类型
                        url: "project/checkpass_all/ids/"+ Config.ids,
                        success: function (ret) {
                            Layer.alert(__('提交成功'), function(index){
                                location.reload();
                            });
                        },
                        error : function(error) {
                            console.log(error);
                        }
                    });
                    Layer.close(index);
                });

            });
            Controller.api.viewchecklog();
        },
        discuss_add: function() {
            // var form = $("form[role=form]");
            Form.api.bindevent($("form[role=form]"));
            $(".pending").click(function(){
                var item_id =$(this).data('id');
                alert("aaaa");
                if(this.checked)
                {
                    $("#"+item_id).val("待定");
                    $("#"+item_id).attr('readonly','readonly');
                }
                else
                {
                    $("#"+item_id).val("");
                    $("#"+item_id).removeAttr('readonly');
                }
            });

        },
        signed_add: function() {

            Form.api.bindevent($("form[role=form]"));
            var pre = Config.isSuperAdmin?"":"tmp_";
            $("#"+pre+"chargedep_ids").val(Config.chargedep_id);
            $(".pending").click(function(){
                var item_id =$(this).data('id');
                if(this.checked)
                {
                    $("#"+item_id).val("待定");
                    $("#"+item_id).attr('readonly','readonly');
                }
                else
                {
                    $("#"+item_id).val("");
                    $("#"+item_id).removeAttr('readonly');
                }
            });
            $('#'+pre + "carrier_id").change(function(event) {

                var value = $(this).val();
                var id = $(this).attr('id');
                // alert(id);
                var unit = '';
                var readonly = false;
                var required = null;
                switch (value) {
                    case "1":
                        unit = "亩";
                        readonly = false;
                        required = "required";
                        break;
                    case "2":
                    case "3":
                    case "4":
                        unit = "平米";
                        readonly = false;
                        required = "required";
                        break;
                    case "5":
                        unit = "";
                        readonly = true;
                        required = null;
                        break;
                }

                $("#"+id+"_addon").html(unit);
                if(value == "5")
                {
                    $("form[role=form]").validator("setField", "row["+id+"_relation]", null);
                    $("#"+id+"_relation").removeAttr('aria-invalid');
                    $("#"+id+"_relation").attr('readonly','readonly');
                }
                else
                {
                    $("form[role=form]").validator("setField", "row["+id+"_relation]", required);
                    $("#"+id+"_relation")..removeAttr('readonly')
                }

            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },

            viewchecklog: function() {
                $(".field_detail").click(function(event) {
                    var that = this;
                    var item_id = $(that).data("item_id");
                    // alert(item_id);
                    var op = {};
                    var filter = {};
                    op.operate_field = '=';
                    filter.operate_field = item_id;

                    var url ='project_checklog/fieldlog?operate_field=' + item_id ;
                    console.log(url);
                    // Backend.api.addtabs(url,'修改记录')
                    // window.open(url, "_newtab");
                    Fast.api.open(url, '', {area:['95%','95%']})
                })
            },
            custom_bindvent: function (form) {
                var events = Form.events;
                events.datetimepicker(form);
                events.selectpicker(form);
            },
            custom_validator: function(form, table) {
                form.on('valid.form', function(e, fform){
                    var type = form.attr("method") ? form.attr("method").toUpperCase() : 'GET';
                    type = type && (type === 'GET' || type === 'POST') ? type : 'GET';
                    url = form.attr("action");
                    console.log(type,url)
                    $.ajax({
                        type: type,
                        url: url,
                        data: form.serialize(),
                        success: function (ret) {
                            Layer.alert(__('添加成功'));
                            table.bootstrapTable('refresh');
                        },
                        error : function(error) {
                            console.log(error);
                        }
                    });
                });
            },
            columns: function(table,show_check,show_edit) {
                var pre_columns = [
                    {checkbox: true},
                    {field: '', title: __('序号'),operate:false,table: table,formatter:Table.api.formatter.natureorder},
                    // {field: 'id', title: __('Id'),operate:false},
                ];
                var operate_columns = [];
                var check_columns = [];
                var common_columns = [];
                if(show_check && Config.isSuperAdmin)
                {

                    check_columns = [
                        {field: 'status',title:'状态',operate:false,
                            formatter: function (value,row,index) {
                                if(value == 0)
                                    return '<a href="javascript:;" class="btn btn-success btn-xs ">已审核</a>';
                                else
                                    return '<a href="project/checkedit/ids/'+row.id+'" class="btn btn-danger   btn-addtabs btn-xs" title="审核">待审核</a>';

                            }
                        }
                    ];
                }
                if(show_edit  )
                {
                    btn_detail = {field: 'operate', title: __('Operate'), align:'left',with:'120px',table: table, events: Table.api.events.operate,
                        buttons: [
                            {
                                name: 'edit',
                                icon: 'fa fa-pencil',
                                text: '编辑',
                                title: __('Edit'),
                                extend: 'data-toggle="tooltip"',
                                classname: 'btn btn-xs btn-success btn-addtabs btn-modi',
                                extend: 'data-icon="fa fa-pencil" data-toggle="tooltip" data-area=\'["100%","100%"]\'',
                                url: 'project/edit',
                                visible:function(row) {
                                    var idx = -1;
                                    if(row.chargedep_ids)
                                    {
                                        var chargedep_ids_arr = row.chargedep_ids.split(',');

                                        idx= chargedep_ids_arr.findIndex(function(item){
                                            return parseInt(item) == parseInt(Config.chargedep_id);
                                        })
                                    }


                                    if(Config.isSuperAdmin || idx!=-1)
                                        return true;
                                    else
                                        return false;
                                }
                            },{
                                name: 'detail',
                                icon: 'fa fa-info',
                                title: __('详情'),
                                text: '详情',
                                url: 'project/detail',
                                extend: 'data-toggle="tooltip"',
                                classname: 'btn btn-xs btn-info btn-addtabs'
                            }
                        ],
                        formatter: Table.api.formatter.buttons

                    };
                }
                else
                {
                    btn_detail = {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [{
                                   name: 'detail',
                                    icon: 'fa fa-info',
                                    text: '详情',
                                    title: __('详情'),
                                    url: 'project/detail',
                                    extend: 'data-toggle="tooltip" data-area=\'["100%","100%"]\'',
                                    classname: 'btn btn-xs btn-info btn-addtabs'
                            }],
                            formatter: Table.api.formatter.buttons
                        };

                }
                operate_columns.push(btn_detail)
                // if(btn_type == 0 || btn_type == 2) {
                //     var btn_operator ={
                //         field: 'check',
                //         width: "120px",
                //         title: __('审核'),
                //         table: table,
                //         events: Table.api.events.operate,
                //         visible: Config.isSuperAdmin ? true:false,
                //         buttons: [

                //             {
                //                 name: 'check',
                //                 title: '审核',
                //                 text: '审核',
                //                 title: __('审核'),
                //                 classname: 'btn btn-xs btn-warning  btn-magic btn-addtabs',
                //                 icon: 'fa fa-folder-o',
                //                 url: 'project/checkedit',
                //             }
                //         ],
                //         formatter: function (value, row, index) {
                //             console.log(Config.isSuperAdmin)
                //             if(Config.isSuperAdmin){
                //                 return Table.api.formatter.buttons.call(this, value, row, index);
                //             }
                //             return '';
                //         }
                //     };
                //     operate_columns.push(btn_operator)
                // }
                // if(Config.isSuperAdmin)
                //     common_columns.push({field: 'memo', title: __('备注'),operate:'like',width:'80px'});

                common_columns.push({field: 'name',width:'100px', title: __('Name'),operate:'like'});
                common_columns.push({field: 'chargedep_names', title: __('提报部门'),operate:false,
                    formatter:Table.api.formatter.delimiter
                });



                common_columns.push({field: 'chargedep_ids', title: __('提报部门'),visible:false,
                            operate:'FIND_IN_SET',
                            searchList: $.getJSON("selectlist/index/tablename/chargedep")
                            // extend:"data-source='chargedep/index'"
                });

                // common_columns.push({field: 'progress_status_id', title: __('项目状态'),
                //     visible:false,
                //     searchList: $.getJSON("selectlist/progress")
                // });
                // common_columns.push({field: 'progress_status_name', title: __('progress_status_name'),
                //     operate:false,
                // });

                common_columns.push({field: 'five_industry_id', title: __('Five_industry_id'),
                    visible:false,
                    searchList: $.getJSON("selectlist/index/tablename/five_industry")
                    // extend:"data-source='five_industry/index'"
                });
                // common_columns.push({field: 'five_industry_name', title: __('Five_industry_id'),
                //     operate:false,
                // });
                common_columns.push({field: 'industry_category_ids', title: __('Industry_category_ids'),
                    visible:false,
                    searchList: $.getJSON("selectlist/index/tablename/industry_category")
                    // extend:"data-source='industry_category/index'"
                });
                common_columns.push({field: 'industry_category_names', title: __('industry_category_ids'),
                    operate:false,
                    formatter:function(value,row,index){
                        str = value.replace(/都不是/g,"");
                        str = str.replace(/,/g,"/");
                        str = str?'/'+str:str;
                        return row.five_industry_name + str;
                    }
                });
                if(Config.library_id == 2)
                {
                    common_columns.push({field: 'registered_capital', title: __('Registered_capital'),
                        operate:'BETWEEN',
                        formatter: Table.api.formatter.price
                    });
                }
                common_columns.push({field: 'investment_total', title: __('Investment_total'), operate:'BETWEEN',
                    formatter:function(value,row,index) {
                        if(value == -100)
                            return '待定';
                        else if(value > 0)
                            return parseFloat(value);
                        else
                            return '';
                    }
                });
                common_columns.push({field: 'domestic_capital', title: __('Domestic_capital'),
                    operate:'BETWEEN',
                    formatter: Table.api.formatter.price
                });
                common_columns.push({field: 'foreign_capital', title: __('Foreign_capital'),
                    operate:'BETWEEN',
                    formatter: Table.api.formatter.price
                });
                // common_columns.push({field: 'asset_type_id', title: __('Asset_type'),
                //     searchList: {"1": __('内资'), "0": __('外资')},
                //     formatter:function(value,row,index)
                //     {
                //         if(value == 1)
                //             return '内资';
                //         else if(value == 2)
                //             return '外资';
                //         else
                //             return '';
                //     }
                // });

                if(Config.library_id == 1)
                {

                    common_columns.push({field: 'progression', title: __('Progression'),operate:false});
                    common_columns.push({field: 'problem', title: __('Problem'),operate:false});
                    common_columns.push({field: 'next_measure', title: __('next_measure'),operate:false});
                    common_columns.push({field: 'access_time', title: __('access_time'),
                        addclass:'datetimerange',
                        visible:false
                    });
                    common_columns.push({field: 'access_time_desc', title: __('access_time'),
                        operate:false,
                    });
                    common_columns.push({field: 'sign_time', title: __('sign_time'),
                        addclass:'datetimerange',
                        visible:false
                    });
                    common_columns.push({field: 'sign_time_desc', title: __('sign_time'),
                       operate:false,
                    });

                }
                if(Config.library_id == 2)
                {

                    common_columns.push({field: 'framework_agreement_time_desc', title: __('framework_agreement_time_desc'),
                        operate:false
                    });
                    common_columns.push({field: 'framework_agreement_time', title: __('framework_agreement_time_desc'),
                        visible:false,
                        addclass:'datetimerange',
                        datetimeFormat:"YYYY-MM-DD",
                    });
                    common_columns.push({field: 'invesment_agreement_time_desc', title: __('invesment_agreement_time_desc'),
                        operate:false
                    });
                    common_columns.push({field: 'invesment_agreement_time', title: __('invesment_agreement_time'),
                        visible:false,
                        addclass:'datetimerange',
                        datetimeFormat:"YYYY-MM",
                    });
                    common_columns.push({field: 'register_time', title: __('register_time'),
                        addclass:'datetimerange',
                        datetimeFormat:"YYYY-MM",
                        formatter: Table.api.formatter.datetime,
                        visible:false
                    });
                    common_columns.push({field: 'register_time_desc', title: __('register_time'),
                        operate:false
                    });
                    common_columns.push({field: 'land_time', title: __('land_time'),
                        addclass:'datetimerange',
                        datetimeFormat:"YYYY-MM",
                        formatter: Table.api.formatter.datetime,
                        visible:false
                    });
                    common_columns.push({field: 'land_time_desc', title: __('land_time'),
                        operate:false
                    });
                    common_columns.push({field: 'start_construction_time', title: __('start_construction_time'),
                        addclass:'datetimerange',
                        datetimeFormat:"YYYY-MM",
                        formatter: Table.api.formatter.datetime,
                        visible:false
                    });
                    common_columns.push({field: 'start_construction_time_desc', title: __('start_construction_time'),
                        operate:false
                    });


                }

                common_columns.push({field: 'investment_name',title: __('investment_name'),operate:'like',
                    formatter:Table.api.formatter.delimiter
                });
                common_columns.push({field: 'investment_area', title: __('investment_area'),operate:'like',
                    formatter:Table.api.formatter.delimiter
                });
                common_columns.push({field: 'investment_attribute_ids', title: __('Investment_attribute_ids'),
                    visible:false,
                    searchList: $.getJSON("selectlist/index/tablename/investor_attribute")
                });
                common_columns.push({field: 'investment_attribute_names', title: __('Investment_attribute_ids'),
                    operate:false,formatter:Table.api.formatter.delimiter
                });
                if(Config.library_id == 1)
                {
                    common_columns.push({field: 'planned_street_ids', title: __('planned_street_ids'),
                        visible:false,
                        searchList: $.getJSON("selectlist/street")
                        // extend:"data-source='industry_category/index'"
                    });
                    common_columns.push({field: 'planned_street_names', title: __('planned_street_ids'),
                        operate:false,formatter:Table.api.formatter.delimiter
                    });
                }
                if(Config.library_id == 2)
                {
                    common_columns.push({field: 'settle_street_ids', title: __('Settle_street_ids'),
                        visible:false,
                        searchList: $.getJSON("selectlist/street")
                        // extend:"data-source='industry_category/index'"
                    });
                    common_columns.push({field: 'settle_street_names', title: __('Settle_street_ids'),
                        operate:false,
                    });
                    common_columns.push({field: 'project_source_ids', title: __('project_source_ids'),
                        visible:false,
                        searchList: $.getJSON("selectlist/index/tablename/activity")
                    });
                    common_columns.push({field: 'project_source_names', title: __('project_source_ids'),
                        operate:false,formatter:Table.api.formatter.delimiter
                    });
                }

                common_columns.push({field: 'city_district_contact', title: __('City_district_contact'),width:"60px",operate:false});


                // var field_columns = pre_columns.concat(common_columns,operate_columns,check_columns);
                var field_columns = pre_columns.concat(common_columns,check_columns);
                return [field_columns];
            },
            check_columns: function(table){
                 var field_columns =
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'pno', title: __('Pno')},
                        // {field: 'sort', title: __('Sort')},
                        {field: 'name', title: __('Name')},
                        {field: 'chargedep_names', title: __('所属街道或部门')},
                        {
                            field: 'check',
                            width: "120px",
                            title: __('审核'),
                            table: table,
                            events: Table.api.events.operate,
                            visible: Config.isSuperAdmin ? true:false,
                            buttons: [
                                {
                                    name: 'check',
                                    text: '审核',
                                    title: __('审核'),
                                    classname: 'btn btn-xs btn-warning  btn-magic btn-addtabs',
                                    icon: 'fa fa-folder-o',
                                    url: 'project/checkedit',
                                }
                            ],
                            formatter: function (value, row, index) {
                                console.log(Config.isSuperAdmin)
                                if(Config.isSuperAdmin){
                                    return Table.api.formatter.buttons.call(this, value, row, index);
                                }
                                return '';
                            }
                        }

                    ];
                    return [field_columns];
            },
            buttons:function(row) {

                console.log(row);
                return [{
                   name: 'detail',
                    icon: 'fa fa-info',
                    title: __('详情'),
                    url: 'project/detail',
                    extend: 'data-toggle="tooltip" data-area=\'["100%","100%"]\'',
                    classname: 'btn btn-xs btn-info btn-addtabs'
                }];
            },
            bindeventTable: function (table) {
                var parenttable = table.closest('.bootstrap-table');
                //Bootstrap-table配置
                var options = table.bootstrapTable('getOptions');
                // console.log(options);
                //Bootstrap操作区
                var toolbar = $(options.toolbar, parenttable);

                table.on('dbl-click-row.bs.table', function (e, row, element, field) {
                    $('.btn-modi', element).trigger("click");
                });
                console.log(toolbar);
                // $(".btn-customedit").click(function(event) {
                //     /* Act on the event */alert("aaaa");
                // });
                // alert("aaaa");
                // console.log(toolbar)
                $(toolbar).on('click', ".btn-customedit", function () {
                    var that = this;
                    //循环弹出多个编辑框
                    $.each(table.bootstrapTable('getSelections'), function (index, row) {
                        var url = options.extend.edit_url;
                        row = $.extend({}, row ? row : {}, {ids: row[options.pk]});
                        var url = Table.api.replaceurl(url, row, table);
                        var icon = 'fa fa-pencil';
                        Backend.api.addtabs(url, '编辑', icon);
                        // Fast.api.open(url, __('Edit'), $(that).data() || {});
                    });
                });
                $(toolbar).on('click', ".btn-detail", function () {
                    var that = this;
                    //循环弹出多个编辑框
                    $.each(table.bootstrapTable('getSelections'), function (index, row) {
                        var url = 'project/detail';
                        row = $.extend({}, row ? row : {}, {ids: row[options.pk]});
                        var url = Table.api.replaceurl(url, row, table);
                        var icon = 'fa fa-info';
                        Backend.api.addtabs(url, '详情', icon);
                        // Fast.api.open(url, __('Edit'), $(that).data() || {});
                    });
                });

            },
            bindSubmitSingle: function(_this, type) {
                // console.log(type);
                type = typeof type === 'undefined' ? 0 : type;
                var item_id = $(_this).data("item_id");

                // var value_name = $("#"+item_id).attr("name");
                var value = $("#"+item_id).val();

                var desc_value = $("#"+item_id+'_submit_desc').val();
                var required = $("#"+item_id+'_submit_desc').data('required');

                var data = {};


                if(type ) {
                    if(!value )
                    {
                        Layer.alert(__('请填写修改内容'));
                        return false;
                    }

                    if(required && !desc_value)
                    {
                         Layer.alert(__('请填写修改原因'));
                         return false;
                    }
                }
                else {
                    if(!value && !desc_value)
                    {
                        Layer.alert(__('您未有要提交的数据'));
                        return false;
                    }
                }
                // console.log(item_id);
                if(item_id == 'carrier_id' )
                {
                    relation_value = $("#"+item_id+'_relation').val();
                    if(value != '5' && relation_value == '')
                    {
                        Layer.alert(__('请填写载体面积'));
                        return false;
                    }
                    data['row[value_relation]'] = relation_value;

                }
                data['row[value]'] = value;
                data['row[desc_value]'] = desc_value;

                data.item_id = item_id;
                if(type)
                    data['row[status]'] = 100;
                $.ajax({
                //几个参数需要注意一下
                    type: "POST",//方法类型
                    dataType: "json",//预期服务器返回的数据类型
                    url: location.href ,//url
                    data: data,
                    success: function (ret) {
                        Layer.alert(__('提交成功'), function(index){
                            location.reload();
                        });
                        // location.reload();
                    },
                    error : function(error) {
                        console.log(error);
                    }
                });

            },
            bindSubmitAll:function(type) {
                type = typeof type === 'undefined' ? 0 : type;
                var errors = [];
                var data = {};
                $(".form_value").each(function(){
                    // console.log($(this))

                    var item_id = $(this).attr('id');
                    var item_value = $(this).val();

                    if(item_id )
                    {
                        // console.log(item_id+','+item_value);
                        var item_label =  $("#"+item_id+"_label").html();
                        var item_value_desc = $("#"+item_id+"_submit_desc").val();
                        var item_required = $("#"+item_id+'_submit_desc').data('required');

                        if(type && item_value && !(item_value=='0.0000') && item_required && !item_value_desc)
                        {
                            errors.push('请填写'+item_label+'修改原因');
                        }

                        if(item_id == 'carrier_id' && item_value && item_value!='5')
                        {
                            var relation_value = $("#"+item_id+"_relation").val();
                            if(!relation_value)
                                errors.push('请填写载体面积');
                        }

                        data['row['+item_id+'][value]'] = item_value;

                        data['row['+item_id+'][desc_value]'] = item_value_desc;
                        if(type && item_value && !(item_value=='0.0000'))
                            data['row['+item_id+'][status]'] = 100;
                    }
                });
                // console.log($.isEmptyObject(data));

                if(type && errors.length > 0){
                    Layer.alert(errors.join("<br>"));
                    return false;
                }

                if($.isEmptyObject(data))
                {
                    Layer.alert('您没有要提交的数据，请填写后再提交');
                    return false;
                }
                data['type'] = type;
                $.ajax({
                //几个参数需要注意一下
                    type: "POST",//方法类型
                    dataType: "json",//预期服务器返回的数据类型
                    url: 'project/editall/ids/' + Config.ids,//url
                    data: data,
                    success: function (ret) {
                        if(ret.code == 1)
                            Layer.alert(__('提交成功'), function(index){
                                location.reload();
                            });
                        else{
                             Layer.alert(ret.msg);
                        }
                        // location.reload();
                    },
                    error : function(error) {
                        console.log(error);
                    }
                })

            },

        }
    };
    return Controller;
});